var express = require("express");
var router = express.Router();
const { crowdModel } = require("../model/zq");
const { mzhModel } = require("../model/mzh");
const judgmentOn = require('../utils/judgmentOn')

const cron = require('cron');
const updArray = (marks) => {
  let newArray = [];
  for (let j = 0; j < marks.length; j++) {
    let obj = {};
    obj.lng = marks[j].longitude;
    obj.lat = marks[j].latitude;
    newArray.push(obj);
  }
  return newArray
}
// 创建CronJob实例
const job = new cron.CronJob('0 0 0 * * *', async () => {
  let user = await mzhModel.find({ username: "admin" })

  let marks = user[0].home
  let newArray = updArray(marks)

  let corowd = await crowdModel.find()

  let { list } = judgmentOn(corowd, newArray)
  await mzhModel.updateOne({ username: "admin" }, { beforeGrazingNum: list.length })

});

// 启动任务
job.start();


router.get("/", async (req, res) => {
  // crowdModel.create({
  //   id: 1,
  //   latitude:"43.6120",
  //   longitude:"87.5060"
  // })
  let page = req.query.page != undefined ? req.query.page : 1;
  let limit = req.query.limit ? req.query.limit : 100;
  let data
  if (page == 0) {
    data = await crowdModel.find()
    let pageNum = await mzhModel.find({ username: "admin" })
    res.send({
      data,
      code: 200,
      num: pageNum[0].num
    });
  } else {
    data = await crowdModel
      .find()
      .skip((page - 1) * limit)
      .limit(limit);
    res.send({
      data,
      code: 200,
    });
  }

});


router.post("/updOne", async (req, res) => {
  let body = req.body;
  let upd = await crowdModel.updateOne(
    { id: req.query.id },
    { body, secure: iftrue({ lat: 43.6121, lng: 87.506 }, a) }
  );
  res.send({
    code: 200,
  });
});
/*参数 data：顶点坐标
 *page页数
 *limit每次多少只羊
 */
router.post("/InquirePage", async (req, res) => {
  let page = req.query.page ? req.query.page : 1;
  let limit = req.query.limit ? req.query.limit : 100;
  let user = await mzhModel.find({ username: 'admin' })
  let corowd = await crowdModel.find().skip((page - 1) * limit).limit(limit);

  let fence = updArray(user[0].fence)
  let warn = updArray(user[0].polygons)
  let safe = updArray(user[0].polygonstwo)

  let { errorList } = judgmentOn(corowd, fence)
  let { errorList: warnList } = judgmentOn(corowd, warn)
  let { list, errorList: safeList } = judgmentOn(corowd, safe)

  res.send({
    code: 200,
    errorList, //危险的羊
    warnList, //警告的羊
    safeList, //安全的羊
    list, //安全圈内的羊
    total: corowd.length, //该圈羊的总数
  });
});
// 更新在羊圈的羊
router.get("/InquireSecurePage", async (req, res) => {

  let user = await mzhModel.find({ username: "admin" })
  let marks = user[0].home
  let newArray = updArray(marks)

  let corowd = await crowdModel.find()

  let { list } = judgmentOn({ corowd, newArray })
  await mzhModel.updateOne({ username: "admin" }, { beforeGrazingNum: list.length })

  res.send({
    code: 200,
    msg: '更新成功',
    num: list.length
  });
});
// 获取羊圈中的羊
router.get("/setNum", (req, res) => {
  mzhModel.find({ username: "admin" }).then(data => {
    res.send({
      code: 200,
      num: data[0].beforeGrazingNum
    })
  })
})

router.post("/updnum", async (req, res) => {
  await mzhModel.updateOne({ username: "admin" }, { num: req.body.num });
  res.send({
    msg: 200,
  });
});
module.exports = router;
